package org.example.algorithm.recursion;

import org.example.algorithm.domain.Person;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Recursion {

    public static void main(String[] args) {
        List<Person> list = Arrays.asList(
                new Person("2", "0", "steven"),
                new Person("3", "1", "steven"),
                new Person("4", "3", "steven"),
                new Person("5", "4", "steven"),
                new Person("6", "5", "steven"),
                new Person("7", "1", "steven"),
                new Person("8", "3", "steven"));

       /* List<Person> personList = getChildCompanyIds("1", list);
        System.out.println(personList);*/

        /*Person person = new Person("1", "0", "steven");
        Person personChild = getPersonChild(person, list);
        System.out.println(personChild);
        int sum = sum(10);
        System.out.println(sum);*/
        int seq = seq(2);
        System.out.println(seq);
    }


    // 模拟获取某个公司的子公司列表
    public static List<Person> getChildCompanies(String id, List<Person> allCompanies) {
        List<Person> childCompanies = new ArrayList<>();
        for (Person company : allCompanies) {
            if (company.getPid().equals(id)) {
                childCompanies.add(company);
            }
        }
        return childCompanies;
    }

    public static List<Person> getChildCompanyIds(String id, List<Person> allCompanies) {
        List<Person> childIds = new ArrayList<>();
        List<Person> childCompanies = getChildCompanies(id, allCompanies);
        for (Person company : childCompanies) {
            childIds.add(company);
            childIds.addAll(getChildCompanyIds(company.getId(), allCompanies));
        }
        return childIds;
    }

    public static Person getPersonChild(Person person, List<Person> personList) {
        List<Person> personChilds = getPersonChild1(person, personList);
        person.setChild(personChilds);
        for (Person person1 : personChilds) {
            List<Person> personChild1 = getPersonChild1(person1, personList);
            person1.setChild(personChild1);
        }
        return person;
    }

    public static List<Person> getPersonChild1(Person person, List<Person> personList) {
        List<Person> persons = new ArrayList<>();
        for (Person person1 : personList) {
            if (person1.getPid().equals(person.getId())){
                persons.add(person1);
            }
        }
        return persons;
    }

    public static int sum(int num){
        int result = 0;
        if (num == 0){
            return 0;
        }
        if (num == 1){
            return 1;
        }
        result = sum(num-1) + sum(num -2);
        System.out.println(result);
        return result;
    }

    public static int seq(int n){
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum+= Math.pow(i, 2);
        }
        return sum;
    }



}
